'use strict';

const BaseService = require('./base');

class UserRelationService extends BaseService {
  get _model() {
    return 'UserRelation';
  }

  async isExist({ uid, followId, paranoid = true }) {
    const where = this.formatWhere({ exact: { uid, followId } });
    return this._isExist({ where, paranoid });
  }

  async create({ uid, followId }) {
    return this._create({
      uid, followId,
    });
  }

  async restore({ uid, followId }) {
    const where = this.formatWhere({ exact: { uid, followId } });
    return this._restore({ where });
  }

  async destory({ id, uid, followId }) {
    const where = this.formatWhere({ id, uid, followId });
    return this._destory({ where });
  }

  async findList({ uid, followId, prop, sort, pageIndex, pageSize, createdAt }) {
    const { app } = this;
    const where = this.formatWhere({ range: { createdAt }, exact: { uid, followId } });
    const order = this.formatOrder(prop, sort);
    const include = [
      {
        model: app.model.User,
        attributes: [ 'nickName' ],
        as: 'User',
        include: [
          {
            model: app.model.UserInfo,
            attributes: [ 'avatar' ],
          },
        ],
      },
      {
        model: app.model.User,
        attributes: [ 'nickName' ],
        as: 'followUser',
        include: [
          {
            model: app.model.UserInfo,
            attributes: [ 'avatar' ],
          },
        ],
      },
    ];
    return this._findList({ pageIndex, pageSize, where, include, order });
  }
}

module.exports = UserRelationService;
